﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using ForrajeriaBCL;

namespace ForrajeriaFRONT.Reportes
{
    public partial class PedidosClientes : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                PedidoClienteBCL pedCBCL = new PedidoClienteBCL();
                
                DataTable dt = new DataTable();
               
                fechaInicial.SelectedDate = DateTime.Today.AddMonths(-1);
                fechaInicial.VisibleDate = DateTime.Today.AddMonths(-1);
                fechaFinal.SelectedDate = DateTime.Today;
                string f1 = fechaInicial.SelectedDate.ToString("dd/MM/yyyy");
                string f2 = fechaFinal.SelectedDate.ToString("dd/MM/yyyy");
                dt = pedCBCL.ReportePedidos(fechaInicio:f1,fechaFin:f2);
                
                gridProveedor.DataSource = dt;
                gridProveedor.DataBind();
                ViewState["lista"] = dt;
                ViewState["columna"] = "";
                ViewState["direccion"] = "";
            }


        }

        protected void gridProveedor_PageIndexChanged(object sender, EventArgs e)
        {
            

        }

        protected void gridProveedor_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridProveedor.DataSource = (DataTable)ViewState["lista"];
            gridProveedor.PageIndex = e.NewPageIndex;
            gridProveedor.DataBind();
        }

        protected void gridProveedor_Sorting(object sender, GridViewSortEventArgs e)
        {
            ViewState["columna"] = e.SortExpression;


            if (ViewState["direccion"].ToString() == "asc")
            {
                ViewState["direccion"] = "desc";
            }
            else
            {
                ViewState["direccion"] = "asc";
            }

            cargarGrilla();
          
           
        }

        protected void gridProveedor_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            PedidoClienteBCL pedBCL = new PedidoClienteBCL();
            DataTable dtDetalle = new DataTable();

            try
            {
                int nro= int.Parse(gridProveedor.Rows[e.NewSelectedIndex].Cells[1].Text);
                dtDetalle = pedBCL.DetalleReporte(nro);
                gridDetalle.DataSource = dtDetalle;
                gridDetalle.DataBind();

            }
            catch
            {
            }
        }

        protected void btnFiltrar_Click(object sender, EventArgs e)
        {
            cargarGrilla();
            gridDetalle.DataSource = null;
            gridDetalle.DataBind();
            gridProveedor.SelectedIndex = -1;

        }

        protected void cargarGrilla()
        {
            PedidoClienteBCL pedBCL = new PedidoClienteBCL();
            DataTable dt = new DataTable();

            try
            {
                string f1 = fechaInicial.SelectedDate.ToString("dd/MM/yyyy");
                string f2 = fechaFinal.SelectedDate.ToString("dd/MM/yyyy");

                dt = pedBCL.ReportePedidos(f1, f2, cmbEstado.SelectedValue, ViewState["columna"].ToString(), ViewState["direccion"].ToString());

                ViewState["lista"] = dt;
                gridProveedor.DataSource = dt;
                gridProveedor.DataBind();

                

            }
            catch
            {
                Response.Redirect("/Default.aspx");
            }
        }

    }
}